/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.controller.classreport;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.annotation.SysLogger;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.controller.BaseController;
import cn.qidisoft.edu.hzjt.model.ClassReport;
import cn.qidisoft.edu.hzjt.model.User;
import cn.qidisoft.edu.hzjt.service.IClassReportService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author hhl
 * @version $Id: LoginController.java, v 0.1 2018年10月22日 下午1:54:25 hhl Exp $
 */
@Controller
@RequestMapping(value = "classReport")
public class ClassReportController {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);
  @Autowired
  private IClassReportService classReportService;

  @RequestMapping(value = "List")
  public String index(ModelMap model, HttpServletRequest request,
      @RequestParam(value = "pageSize", required = false) String pageSize,
      @RequestParam(value = "currentPage", required = false) String currentPage,
      ClassReport classreport) {
    User user = (User) request.getSession().getAttribute(Constant.LOGIN_CONSOLE_USER);
    try {
      Page page = new Page();
      page.setCurrPage(currentPage == null ? 0 : Integer.valueOf(currentPage));
      page.setPageSize(pageSize == null ? 10 : Integer.valueOf(pageSize));
      BaseController.setSortByColumn(request, "type", "asc");
      classreport.setOrderField((String) request.getAttribute("orderField"));
      classreport.setOrderFlag((String) request.getAttribute("orderFlag"));
      PaginationBean<ClassReport> paginationBean =
          classReportService.queryPageList(classreport, page);
      model.addAttribute("list", paginationBean.getPageList());
      model.addAttribute("listPage", paginationBean);
      request.setAttribute("msg", BaseController.getParameter("msg"));
      BaseController.removeParameter("msg");

      model.addAttribute("user", user);
      model.addAttribute("data", classreport);
      return "classReport/classReportList";
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }

  }

  @RequestMapping(value = "/addInInt", method = RequestMethod.GET)
  public String add(ModelMap model, HttpServletRequest request) {

    return "classReport/addClassReport";
  }

  @RequestMapping(value = "/updateToview", method = RequestMethod.GET)
  public String updateToview(ModelMap model, HttpServletRequest request,
      @RequestParam(value = "id") Integer id) {
    try {
      ClassReport data = new ClassReport();
      data.setId(id);
      data = classReportService.queryByPK(data);
      model.addAttribute("data", data);

      return "classReport/updateClassReport";
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }
  }

  @SysLogger(description = "顶班申请信息", moduleName = "顶班申请管理", method = "新增")
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public String add(ModelMap model, HttpServletRequest request, ClassReport data) {
    try {
      data.setType(0);
      classReportService.add(data);
      BaseController.setParameter("msg", "保存成功");
      return "redirect:List";
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
      e.printStackTrace();
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }
  }

  @SysLogger(description = "顶班申请信息", moduleName = "顶班申请管理", method = "删除")
  @RequestMapping(value = "/delete", method = RequestMethod.POST)
  public String delete(ModelMap model, HttpServletRequest request) {
    try {
      String[] idList = request.getParameter("idList").split(",");
      for (String id : idList) {
        ClassReport data = new ClassReport();
        data.setId(Integer.valueOf(id));
        classReportService.delete(data);
      }
      BaseController.setParameter("msg", "删除成功");
      return "redirect:List";
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }
  }

  @SysLogger(description = "顶班申请信息", moduleName = "顶班申请管理", method = "修改")
  @RequestMapping(value = "/update", method = RequestMethod.POST)
  public String update(ModelMap model, HttpServletRequest request, ClassReport data) {
    try {
      data.setType(0);
      classReportService.update(data);
      BaseController.setParameter("msg", "保存成功");
      return "redirect:List";
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }
  }
}
